package com.irdstudio.bsp.executor.core.plugin.migrate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import com.irdstudio.bsp.executor.core.dao.MigrateTableInfoDao;
import com.irdstudio.bsp.executor.core.dao.domain.MigrateTableInfo;
import com.irdstudio.bsp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.bsp.executor.core.plugin.common.SSubsDatasource;
import com.irdstudio.bsp.executor.core.plugin.common.SSubsDatasourceDao;
import com.irdstudio.bsp.executor.core.plugin.migrate.dao.MigrateFieldMapping;
import com.irdstudio.bsp.executor.core.plugin.migrate.dao.MigrateFieldMappingDao;
import com.irdstudio.bsp.executor.core.plugin.migrate.dao.MigrateTableMapping;
import com.irdstudio.bsp.executor.core.plugin.migrate.dao.MigrateTableMappingDao;
import com.mysql.jdbc.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * 
 * 迁移表数据插件:
 * 1、源到到目标表迁移，根据配置进行迁移
 * 2、SQL语句到目标表迁移，根据配置进行迁移
 * @author ligm
 *
 */
public class MigrateTableDataPlugin extends AbstractPlugin {
	
	/** 表数据迁移映射配置 */
	private MigrateTableMapping tableMapping = null;

	/** 表字段映射配置信息 */
	private List<MigrateFieldMapping> fieldMappingList = null;

	
	@Override
	protected boolean doReadConfigureFromDB(Connection conn, String szConfIdentify) throws SQLException {
		//获取插件配置数据示例写法，如下：		
		// 根据表映射标识获取配置信息
		String tableMappingid = szConfIdentify;
		MigrateTableMappingDao tableDao = new MigrateTableMappingDao(conn);
		String szCondition=" where table_mapping_id='"+tableMappingid+"'";
		tableMapping = tableDao.queryMigrateTableMappingWithCond(szCondition, "").get(0);

		//根据表映射标识获取字段映射配置
		MigrateFieldMappingDao fieldDao = new MigrateFieldMappingDao(conn);
		String fieldszCondition=" where table_mapping_id='"+tableMappingid+"'";
		fieldMappingList = fieldDao.queryMigrateFieldMappingWithCond(fieldszCondition,"");

		return true;
	}
	
	@Override
	public boolean execute() {
		boolean reulst = false;
		try {
			String source_ds_conn_str = "";
			String target_ds_conn_str = "";
			String subs_ds_code1 = "";
			String subs_ds_code2 = "";
			String source_user_id = "";
			String target_user_id = "";
			String source_pwd = "";
			String target_pwd = "";
			String sourceTableId = this.tableMapping.getSourceTableId();
			String targetTableId = this.tableMapping.getTargetTableId();
			MigrateTableInfo migrateTableInfo = new MigrateTableInfo();
			MigrateTableInfo migrateTableInfo1 = new MigrateTableInfo();

			//查询源表配置信息
			migrateTableInfo.setTableId(sourceTableId);
			MigrateTableInfoDao migrateTableInfoDao = new MigrateTableInfoDao(this.getPluginConnection());
			migrateTableInfo1 = migrateTableInfoDao.queryByPk(migrateTableInfo);
			//获取源表 数据源代码
			if(migrateTableInfo1 != null){
				subs_ds_code1 = migrateTableInfo1.getSubsDsCode();
			}

			//查询目标表配置信息
			migrateTableInfo.setTableId(targetTableId);
			migrateTableInfo1 = migrateTableInfoDao.queryByPk(migrateTableInfo);
			//获取目标表 数据源代码
			if(migrateTableInfo1 != null){
				subs_ds_code2 = migrateTableInfo1.getSubsDsCode();
			}

			//获取数据源信息
			SSubsDatasource sSubsDatasource = new SSubsDatasource();
			SSubsDatasourceDao subsDatasourceDao = new SSubsDatasourceDao(this.getPluginConnection());
			List<SSubsDatasource> list = subsDatasourceDao.queryAll();
			if(list != null && list.size()>0){
				for (int i=0;i<list.size();i++){
					sSubsDatasource = list.get(i);
					String subs_ds_code = sSubsDatasource.getSubsDsCode();
					if(subs_ds_code1.equals(subs_ds_code) || subs_ds_code1 == subs_ds_code){//如果查到源表配置数据库链接
						source_ds_conn_str = sSubsDatasource.getDsConnStr();
						source_user_id = sSubsDatasource.getDsUserId();
						source_pwd = sSubsDatasource.getDsUserPwd();
					}
					if(subs_ds_code2.equals(subs_ds_code) || subs_ds_code2 == subs_ds_code){//如果查到目标表配置数据库链接
						target_ds_conn_str = sSubsDatasource.getDsConnStr();
						target_user_id = sSubsDatasource.getDsUserId();
						target_pwd = sSubsDatasource.getDsUserPwd();
					}
				}

			}

			String source_url = source_ds_conn_str;
			String target_url = target_ds_conn_str;
			//获取源表 数据源的数据库链接
			Driver driver  =  new com.mysql.jdbc.Driver();
			Properties info = new Properties();
			info.setProperty("user",source_user_id);
			info.setProperty("password",source_pwd);
			Connection source_conn = driver.connect(source_url, info);
			//获取目标表 数据源的数据库链接
			info.setProperty("user",target_user_id);
			info.setProperty("password",target_pwd);
			Connection target_conn = driver.connect(target_url, info);

			// 在此完成具体表数据迁移插件的逻辑
			migrateTableDataUtil  migrateTableDataUtil=new migrateTableDataUtil();
			//表数据迁移执行逻辑方法
			reulst=migrateTableDataUtil.datamigration(tableMapping,fieldMappingList,this.getPluginConnection(),source_conn,target_conn);

		}catch(Exception e){
			System.out.println("获取数据库链接错误："+e);
		}
		return reulst;
	}
}
